import 'package:dio/dio.dart';

class ApiResponse<T> {
  final int code;
  final String message;
  final T? data;

  ApiResponse({required this.code, required this.message, this.data});

  factory ApiResponse.fromResponse(Response response, {T Function(dynamic)? fromJson}) {
    final responseData = response.data;
    return ApiResponse(
      code: responseData['code'] ?? 500,
      message: responseData['message'] ?? 'Unknown error',
      data: responseData['data'] != null && fromJson != null
          ? fromJson(responseData['data'])
          : responseData['data'],
    );
  }

  bool get isSuccess => code == 200;
}

class PaginationData<T> {
  final List<T> list;
  final int total;
  final int page;
  final int pageSize;

  PaginationData({
    required this.list,
    required this.total,
    required this.page,
    required this.pageSize,
  });

  factory PaginationData.fromJson(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJson,
  ) {
    final List<dynamic> listData = json['list'] ?? [];
    return PaginationData(
      list: listData.map((item) => fromJson(item as Map<String, dynamic>)).toList(),
      total: json['total'] ?? 0,
      page: json['page'] ?? 1,
      pageSize: json['page_size'] ?? 10,
    );
  }
}